<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Routing\Route;
use League\Flysystem\Exception;
use Request;
use Redirect;
use WindowsAzure\Table\Models\Entity;
use WindowsAzure\Table\Models\EdmType;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\Common\ServicesBuilder;
use Azure;
use HTTP_Request2_ConnectionException;

class MainController extends Controller
{
    var $blob;

    public function index()
    {
        $data['title'] = "Dashboard";
        $data['heading'] = "Dashboard";
        $count = new BlobsController();
        $data['countContainers'] = $count->countContainers();
        return view('dashboard', $data);
    }

    public function home()
    {
        return view('layouts.home');
    }

    public function login()
    {
        $data = Request::all();
        try {
            $connection = "DefaultEndpointsProtocol=https;AccountName=" . $data['account'] . ";AccountKey=" . $data['access'];
            $this->blob = Azure::getInstance()->createBlobService($connection);
            $containers_list = $this->blob->listContainers();
            $containers = $containers_list->getContainers();
            session(['account' => $data['account']]);
            session(['access-key' => $data['access']]);
            $data['statusCode'] = 200;
            return json_encode($data);
        }
        catch (HTTP_Request2_ConnectionException $e) {
            $code = $e->getCode();
            $error_message = $e->getMessage();
            $data['statusCode'] = 400;
            $data['errorMessage'] = $error_message;
            return json_encode($data);
        }
        catch (ServiceException $e) {
            $code = $e->getCode();
            $error_message = $e->getMessage();
            $data['statusCode'] = 400;
            $data['errorMessage'] = $error_message;
            return json_encode($data);
        }
    }

    public function logout()
    {
        session()->forget('account');
        session()->forget('access-key');
        session()->flash('status','Logged out Successfully');
        return Redirect::to('/');
    }

    public function test(){
        $st = "GET\n". /*HTTP Verb*/
        "\n".    /*Content-Encoding*/
        "\n".   /*Content-Language*/
            "\n".   /*Content-Length (include value when zero)*/
            "\n".   /*Content-MD5*/
            "\n".   /*Content-Type*/
            "\n".   /*Date*/
            "\n".  /*If-Modified-Since */
            "\n".   /*If-Match*/
            "\n".   /*If-None-Match*/
            "\n".   /*If-Unmodified-Since*/
            "\n".  /*Range*/
"x-ms-date:Sun, 11 Oct 2009 21:49:13 GMT\nx-ms-version:2009-09-19\n".    /*CanonicalizedHeaders*/
        "/xanstudios /xanstudios\ncomp:list";    /*CanonicalizedResource*/

        $sig = hash_hmac('sha256',$st,"YsyvE3/RrWaYm2mWU5QmFh6TDdp21/Wj7+i+d0d31jnmKapd0Fu1/y46/3+AV1mDcWh7iFz2e995yA7avw9SVA==");
        echo $sig;
        $ch = curl_init('https://xanstudios.file.core.windows.net/?comp=list');
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_HTTPHEADER,array('Authorization','xanstudios:'.$sig));
        $result = curl_exec($ch);
        var_dump($result);
        curl_close($ch);
    }
}
